﻿#include "BrowserTableHandlerLoadConstraintShell.h"
#include "OperatorsInterface/OperAbstractLoad.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsLoadManager.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKLoadConstraintSEAShell.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsAbstractSpectra.h"
#include "OperatorsInterface/GraphEventOperator.h"
#include <QCheckBox>
#include "BrowserTableHandlerLoadPowerShell.h"

namespace GUI
{
    void BrowserTableHandlerLoadConstraintShell::updateTable()
    {
        if (_tableWidget == nullptr) return;
        _tableWidget->clear();
        _tableWidget->setColumnCount(0);
        _tableWidget->setRowCount(0);

        //设置表头
        QStringList headerLabels;
        headerLabels << tr("Name") << tr("Enabled") << tr("Subsystem")
            << tr("Extensional") << tr("shear") << tr("Flexural")
            << tr("Torsional") << tr("Notes");
        _tableWidget->setColumnCount(headerLabels.size());
        _tableWidget->setHorizontalHeaderLabels(headerLabels);
        //更新表格
        this->updateData();
    }

    void BrowserTableHandlerLoadConstraintShell::updateData()
    {
        if (_tableWidget == nullptr) return;
        if (_loadMgr == nullptr)return;

        QList<Acoustics::FITKAcousticsAbstractLoad*> list = _loadMgr->getLoadByType(
            Acoustics::FITKAcousticsAbstractLoad::ConstraintSEAShell);
        //载荷数量
        const int n = list.size();
        _tableWidget->setRowCount(n);

        for (int i = 0; i < n; ++i)
        {
            //转换板功率
            Acoustics::FITKLoadConstraintSEAShell* load = dynamic_cast<Acoustics::FITKLoadConstraintSEAShell*>(list.at(i));
            if (load == nullptr) continue;

            //数据ID
            const int id = load->getDataObjectID();

            _tableWidget->setItem(i, 0, new QTableWidgetItem(load->getDataObjectName(), id));
            QCheckBox* box = new QCheckBox("");
            box->setChecked(load->getEnabledState());
            connect(box, &QCheckBox::stateChanged, this, [load, id](int state) {
                if (state == Qt::Checked)  load->setEnabledState(true);
                else if (state == Qt::Unchecked)  load->setEnabledState(false);
                EventOper::GraphEventOperator* operGraph = FITKOPERREPO->getOperatorT<EventOper::GraphEventOperator>("GraphPreprocess");
                if (operGraph)operGraph->updateGraph(id);
            });
            _tableWidget->setCellWidget(i, 1, box);
            _tableWidget->setItem(i, 2, new QTableWidgetItem(load->getConnectionObjName(), id));

            QString forceMoment = "None";
            if (load->getExtensionSpectra()) forceMoment = load->getExtensionSpectra()->getDataObjectName();
            _tableWidget->setItem(i, 3, new QTableWidgetItem(forceMoment, id));
            forceMoment = "None";
            if (load->getShearSpectra()) forceMoment = load->getShearSpectra()->getDataObjectName();
            _tableWidget->setItem(i, 4, new QTableWidgetItem(forceMoment, id));
            forceMoment = "None";
            if (load->getFlexureSpectra()) forceMoment = load->getFlexureSpectra()->getDataObjectName();
            _tableWidget->setItem(i, 5, new QTableWidgetItem(forceMoment, id));

            _tableWidget->setItem(i, 6, new QTableWidgetItem(load->getNotes(), id));
        }
        _tableWidget->autoResizeCellWidth();
        _tableWidget->setTableItemEditable(false);
    }

    EventOper::OperAbstractLoad * BrowserTableHandlerLoadConstraintShell::getLoadOper()
    {
        //从操作器仓库获取
        return Core::FITKOperatorRepo::getInstance()->
            getOperatorT<EventOper::OperAbstractLoad>("OperLoadConstraintShell");
    }
    bool BrowserTableHandlerLoadConstraintShell::enableAttriSet()
    {
        //再说
        return false;
    }
}